package com.example.springboot.test;

import com.example.springboot.request.CategoryQueryRequest;
import com.example.springboot.request.CategorySaveRequest;
import com.example.springboot.response.CommonResponse;
import com.example.springboot.response.CategoryQueryResponse;
import com.example.springboot.response.PageResponse;
import com.example.springboot.service.ServiceCategory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController//表示访问这个地址时要返回一个字符串
public class CategoryController {

    //查询all
    @Resource
    private ServiceCategory serviceCategory;
    @GetMapping("/Category/all")
    public CommonResponse all(){
        CommonResponse<List<CategoryQueryResponse>> response=new CommonResponse<>();
       List<CategoryQueryResponse> list= serviceCategory.all();
        response.setContent(list);
        return response;
    }

    //查询2
    @GetMapping("/Category/service")
    public CommonResponse list(@Valid CategoryQueryRequest categoryQueryRequest){
        CommonResponse<PageResponse<CategoryQueryResponse>> response=new CommonResponse<>();
        PageResponse<CategoryQueryResponse> list= serviceCategory.list(categoryQueryRequest);
        response.setContent(list);
        return response;
    }

    //保存
    @PostMapping("/Category/save")
    public CommonResponse save(@Valid @RequestBody CategorySaveRequest categoryQueryRequest){
        CommonResponse response=new CommonResponse<>();
        serviceCategory.save(categoryQueryRequest);
        return response;
    }

    //删除
    @DeleteMapping("/Category/delete/{id}")
    public CommonResponse delete(@PathVariable Long id){
        CommonResponse response=new CommonResponse<>();
        serviceCategory.delete(id);
        return response;
    }
}
